package RunnableOldClass;





import RunableClass.useClass.settable;
import RunableSt.authenticator;
import RunnableOldClass.GETThread;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Properties;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 *
 * @author TOMMYNATO
 */
public class News  implements Runnable {
int count=0;static StringBuffer content = new StringBuffer("");
   int count1=1;public static int avg=0;
    /**
     * @param args the command line arguments
     */
  
     public News() {
         
     
    }
    // public static void main(String[]args){
      //    java.awt.EventQueue.invokeLater(new Runnable() {
      ///    public void run() { 
      //        javax.swing.Timer indexStart2 = new javax.swing.Timer(3000, new News()); 
    ///  indexStart2.start();    
    ///  javax.swing.Timer indexStart3 = new javax.swing.Timer(4000, new settable()); 
    ///    indexStart3.start();   
             // new STOCK().setVisible(true);
    //      }});
  //      
  ///  }

    @Override
    public void run() {
        
          javax.swing.Timer WorkControl = new javax.swing.Timer(500, new SetNews());  
        WorkControl.start();
          for(;;){ 
              
          
         ++count;    
    GETThread.jLabel15.setText(String.valueOf(count));
           content = new StringBuffer("");int count2=1;
           try {
          //  URL sUrl = new URL("http://www.settrade.com/C13_MarketSummary.jsp");
           // System.out.println("sURL " + sUrl);
                
         String buffer;
          String url = "http://www.efinancethai.com/LastestNews/AllLastestNews.aspx?type=all",
               proxy = "proxy.up.ac.th",
               port = "8080",
               username = "up\\51286885",
               password = "7489250721";

            //URL autoIP = new URL("http://checkip.dyndns.org:8245/");
           // System.out.println("Get URL");
           
          URL server = new URL(url);

            Authenticator.setDefault(new authenticator(
                        username,password));
         Properties systemProperties = System.getProperties();
           systemProperties.setProperty("http.proxyHost",proxy);
           systemProperties.setProperty("http.proxyPort",port);
            HttpURLConnection connection = (HttpURLConnection)server.openConnection();
           connection.connect();
       // URLConnection conn = sUrl.openConnection();
            PrintStream out;
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"TIS-620")) ;
               
               
                while((buffer = in.readLine()) != null) {
                       count2++;
                   // avg=(count2/count)*100;
                        
                       content.append(count2).append("-->").append(buffer).append("\n");
                       
                  }
            
           //System.out.println("========================");
           //System.out.println(content.toString() + "\n");
            out = new PrintStream(new FileOutputStream("NewsFile.txt"));
            out.println(content.toString());
            out.close();
      //  GETINDEXSTOCK.jProgressBar1.setValue(25);
        
      
      //  GETINDEXSTOCK.jProgressBar1.setValue(100);
               
          // System.out.println("========================");
        } catch (Exception ez){
            System.err.println("Couldn't create help URL: " + ez);
        

    }// TODO add your handling code here:
       
    settable a=new settable("NewsFile.txt"); 
   a.table();
   
        try {
               
           //  ReadRank.SetDb();
            Thread.sleep(5000);
        } catch (InterruptedException ex) {
        }
        
        
    }
    
    
    }
     
     
     
}
class SetNews implements ActionListener{
int count=1;
    @Override
    public void actionPerformed(ActionEvent ae) {
      // System.out.println("--");
               
        if(count==1){
           GETThread.jLabel16.setText("Working."); 
           count++;
        }else if(count==2){
            GETThread.jLabel16.setText("Working.."); 
             count++;
        }else if(count==3){
            GETThread.jLabel16.setText("Working..."); 
            count=1;
        }
      
    }}
    
    
